package com.start.workflow.core.dispatcher;

import com.start.common.std.StartEventDispatcherListener;
import com.start.common.std.StartSingletonFactory;
import com.start.common.tools.SpringContextTools;
import com.start.workflow.core.event.StartWorkFlowInstanceEvent;
import com.start.workflow.entity.StartActBusinessEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * 流程实例事件
 */
@Slf4j
public class StartGlobalActInstEventDispatcher extends StartEventDispatcherListener<StartGlobalActInstEventDispatcher, StartWorkFlowInstanceEvent> implements StartWorkFlowInstanceEvent {

    public static StartGlobalActInstEventDispatcher getInstance() {
        return StartSingletonFactory.get(StartGlobalActInstEventDispatcher.class);
    }


    /**
     * 初始化分发器
     */
    @Override
    protected void initDispatcher() {
        Map<String, StartWorkFlowInstanceEvent> beans = SpringContextTools.getBeans(StartWorkFlowInstanceEvent.class, false);
        if (beans == null || beans.isEmpty()) {
            return;
        }
        Collection<StartWorkFlowInstanceEvent> values = beans.values();
        for (StartWorkFlowInstanceEvent listener : values) {
            if (StringUtils.isNotBlank(listener.key())) {
                addEvent(UNION_KEY_PREFIX, listener.key(), listener);
                continue;
            }
            if (StringUtils.isNotBlank(listener.categoryKey())) {
                addEvent(UNION_KEY_PREFIX, listener.categoryKey(), listener);
                continue;
            }
            addEvent(GLOBAL_PREFIX, GLOBAL_KEY, listener);
        }
    }


    /**
     * 任务创建
     *
     * @param actBusinessEntity
     */
    @Override
    public void onActInstanceCreateBefore(StartActBusinessEntity actBusinessEntity) {
        List<StartWorkFlowInstanceEvent> events = getEvents(UNION_KEY_PREFIX, actBusinessEntity.getProcDefKey());
        if (!events.isEmpty()) {
            for (StartWorkFlowInstanceEvent event : events) {
                event.onActInstanceCreateBefore(actBusinessEntity);
            }
        }
        events = getEvents(GROUP_PREFIX, actBusinessEntity.getProcDefCategory());
        if (!events.isEmpty()) {
            for (StartWorkFlowInstanceEvent event : events) {
                event.onActInstanceCreateBefore(actBusinessEntity);
            }
        }
        events = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (!events.isEmpty()) {
            for (StartWorkFlowInstanceEvent event : events) {
                event.onActInstanceCreateBefore(actBusinessEntity);
            }
        }
    }

    /**
     * 流程创建后
     *
     * @param actBusinessEntity
     */
    @Override
    public void onActInstanceCreateAfter(StartActBusinessEntity actBusinessEntity) {
        List<StartWorkFlowInstanceEvent> events = getEvents(UNION_KEY_PREFIX, actBusinessEntity.getProcDefKey());
        if (!events.isEmpty()) {
            for (StartWorkFlowInstanceEvent event : events) {
                event.onActInstanceCreateAfter(actBusinessEntity);
            }
        }
        events = getEvents(GROUP_PREFIX, actBusinessEntity.getProcDefCategory());
        if (!events.isEmpty()) {
            for (StartWorkFlowInstanceEvent event : events) {
                event.onActInstanceCreateAfter(actBusinessEntity);
            }
        }
        events = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (!events.isEmpty()) {
            for (StartWorkFlowInstanceEvent event : events) {
                event.onActInstanceCreateAfter(actBusinessEntity);
            }
        }
    }

    /**
     * 流程取消前
     *
     * @param actBusinessEntity
     */
    @Override
    public void onActInstanceCancelBefore(StartActBusinessEntity actBusinessEntity) {
        List<StartWorkFlowInstanceEvent> events = getEvents(UNION_KEY_PREFIX, actBusinessEntity.getProcDefKey());
        if (!events.isEmpty()) {
            for (StartWorkFlowInstanceEvent event : events) {
                event.onActInstanceCancelBefore(actBusinessEntity);
            }
        }
        events = getEvents(GROUP_PREFIX, actBusinessEntity.getProcDefCategory());
        if (!events.isEmpty()) {
            for (StartWorkFlowInstanceEvent event : events) {
                event.onActInstanceCancelBefore(actBusinessEntity);
            }
        }
        events = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (!events.isEmpty()) {
            for (StartWorkFlowInstanceEvent event : events) {
                event.onActInstanceCancelBefore(actBusinessEntity);
            }
        }
    }

    /**
     * 流程取消后
     *
     * @param actBusinessEntity
     */
    @Override
    public void onActInstanceCancelAfter(StartActBusinessEntity actBusinessEntity) {
        List<StartWorkFlowInstanceEvent> events = getEvents(UNION_KEY_PREFIX, actBusinessEntity.getProcDefKey());
        if (!events.isEmpty()) {
            for (StartWorkFlowInstanceEvent event : events) {
                event.onActInstanceCancelAfter(actBusinessEntity);
            }
        }
        events = getEvents(GROUP_PREFIX, actBusinessEntity.getProcDefCategory());
        if (!events.isEmpty()) {
            for (StartWorkFlowInstanceEvent event : events) {
                event.onActInstanceCancelAfter(actBusinessEntity);
            }
        }
        events = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (!events.isEmpty()) {
            for (StartWorkFlowInstanceEvent event : events) {
                event.onActInstanceCancelAfter(actBusinessEntity);
            }
        }
    }

    /**
     * 流程结束前
     *
     * @param actBusinessEntity
     */
    @Override
    public void onActInstanceCompleteBefore(StartActBusinessEntity actBusinessEntity) {
        List<StartWorkFlowInstanceEvent> events = getEvents(UNION_KEY_PREFIX, actBusinessEntity.getProcDefKey());
        if (!events.isEmpty()) {
            for (StartWorkFlowInstanceEvent event : events) {
                event.onActInstanceCompleteBefore(actBusinessEntity);
            }
        }
        events = getEvents(GROUP_PREFIX, actBusinessEntity.getProcDefCategory());
        if (!events.isEmpty()) {
            for (StartWorkFlowInstanceEvent event : events) {
                event.onActInstanceCompleteBefore(actBusinessEntity);
            }
        }
        events = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (!events.isEmpty()) {
            for (StartWorkFlowInstanceEvent event : events) {
                event.onActInstanceCompleteBefore(actBusinessEntity);
            }
        }
    }

    /**
     * 流程结束后
     *
     * @param actBusinessEntity
     */
    @Override
    public void onActInstanceCompleteAfter(StartActBusinessEntity actBusinessEntity) {
        List<StartWorkFlowInstanceEvent> events = getEvents(UNION_KEY_PREFIX, actBusinessEntity.getProcDefKey());
        if (!events.isEmpty()) {
            for (StartWorkFlowInstanceEvent event : events) {
                event.onActInstanceCompleteAfter(actBusinessEntity);
            }
        }
        events = getEvents(GROUP_PREFIX, actBusinessEntity.getProcDefCategory());
        if (!events.isEmpty()) {
            for (StartWorkFlowInstanceEvent event : events) {
                event.onActInstanceCompleteAfter(actBusinessEntity);
            }
        }
        events = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (!events.isEmpty()) {
            for (StartWorkFlowInstanceEvent event : events) {
                event.onActInstanceCompleteAfter(actBusinessEntity);
            }
        }

//        StartActCarbonCopyManage.getInstance().getActCarbonCopy();
    }


    /**
     * 任务驳回前
     *
     * @param actBusinessEntity
     */
    @Override
    public void onActInstanceRejectBefore(StartActBusinessEntity actBusinessEntity) {
        List<StartWorkFlowInstanceEvent> events = getEvents(UNION_KEY_PREFIX, actBusinessEntity.getProcDefKey());
        if (!events.isEmpty()) {
            for (StartWorkFlowInstanceEvent event : events) {
                event.onActInstanceRejectBefore(actBusinessEntity);
            }
        }

        events = getEvents(GROUP_PREFIX, actBusinessEntity.getProcDefCategory());
        if (!events.isEmpty()) {
            for (StartWorkFlowInstanceEvent event : events) {
                event.onActInstanceRejectBefore(actBusinessEntity);
            }
        }
        events = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (!events.isEmpty()) {
            for (StartWorkFlowInstanceEvent event : events) {
                event.onActInstanceRejectBefore(actBusinessEntity);
            }
        }
    }

    /**
     * 任务驳回
     *
     * @param actBusinessEntity
     */
    @Override
    public void onActInstanceRejectAfter(StartActBusinessEntity actBusinessEntity) {
        List<StartWorkFlowInstanceEvent> events = getEvents(UNION_KEY_PREFIX, actBusinessEntity.getProcDefKey());
        if (!events.isEmpty()) {
            for (StartWorkFlowInstanceEvent event : events) {
                event.onActInstanceRejectAfter(actBusinessEntity);
            }
        }

        events = getEvents(GROUP_PREFIX, actBusinessEntity.getProcDefCategory());
        if (!events.isEmpty()) {
            for (StartWorkFlowInstanceEvent event : events) {
                event.onActInstanceRejectAfter(actBusinessEntity);
            }
        }
        events = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (!events.isEmpty()) {
            for (StartWorkFlowInstanceEvent event : events) {
                event.onActInstanceRejectAfter(actBusinessEntity);
            }
        }
    }


}
